package com.vmladenov.cook.core.db;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.vmladenov.cook.core.GlobalStrings;
import com.vmladenov.cook.core.converters.SimpleViewConverter;
import com.vmladenov.cook.core.html.SmallPreview;
import com.vmladenov.cook.core.objects.SimpleView;
import com.vmladenov.cook.core.objects.UsefulView;

public class AdvicesRepository implements IRepository
{
	private final SQLiteDatabase db;

	SQLiteStatement maxIdStatement;

	private static final String MAX_ID_SQL = "SELECT COUNT(ID) FROM "
			+ GlobalStrings.ADVICE_TABLE_NAME + " WHERE CATEGORY_ID = %d ";

	private static final String ADVICE_SELECT_CLAUSE = "SELECT ID, ADVICE_ID, URL, "
			+ "IMAGE_URL, TITLE, DESCRIPTION " + " FROM " + GlobalStrings.ADVICE_TABLE_NAME;

	private static final String ADVICE_PAGE_SQL = ADVICE_SELECT_CLAUSE
			+ "  WHERE CATEGORY_ID = %d ";

	private static final String ADVICE_SQL = ADVICE_SELECT_CLAUSE + "  WHERE ID = %d ";

	private static final String ADVICE_CATEGORIES_SQL = "SELECT ID, TITLE FROM ADVICE_CATEGORIES";
	private long categoryId;

	public AdvicesRepository(SQLiteDatabase db)
	{
		this.db = db;

	}

	@Override
	public long getMaxId()
	{
		String sql = String.format(MAX_ID_SQL, categoryId);
		maxIdStatement = db.compileStatement(sql);
		return maxIdStatement.simpleQueryForLong();
	}

	@Override
	public ArrayList<SmallPreview> getPreviews(long start)
	{
		String sql = String.format(ADVICE_PAGE_SQL, categoryId);
		return getAdvicePreviews(sql);
	}

	private ArrayList<SmallPreview> getAdvicePreviews(String sql)
	{
		ArrayList<SmallPreview> previews = new ArrayList<SmallPreview>();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst())
		{
			do
			{
				SmallPreview preview = createAdvicePreview(cursor);
				previews.add(preview);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) cursor.close();
		return previews;
	}

	private SmallPreview createAdvicePreview(Cursor cursor)
	{
		SmallPreview preview = new SmallPreview();
		preview.setId(cursor.getLong(0));
		preview.setTitle(cursor.getString(4)); // Title
		preview.setBigViewUrl(cursor.getString(2)); // URL
		String imageUrl = cursor.getString(3);
		if (imageUrl != null)
		{
			// String imageName = imageUrl.substring(imageUrl.lastIndexOf('/') +
			// 1);
			preview.setImageUrl(imageUrl);
			// try
			// {
			// preview.setCachedImage(Helpers.getSmallAdviceImage(context,
			// imageName));
			// }
			// catch (IOException e)
			// {
			// e.printStackTrace();
			// }
		}
		return preview;
	}

	public ArrayList<SimpleView> getCategories()
	{
		return SQLHelper.ExecuteSql(ADVICE_CATEGORIES_SQL, db, new SimpleViewConverter());
	}

	public void close()
	{
		if (maxIdStatement != null) maxIdStatement.close();
	}

	public void setCategoryId(long categoryId)
	{
		this.categoryId = categoryId;
	}

	public UsefulView getAdviceView(long id)
	{
		UsefulView view = new UsefulView();
		String sql = String.format(ADVICE_SQL, id);
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst())
		{
			view.setId(id);
			view.setTitle(cursor.getString(4));
			view.setDescription(cursor.getString(5));
			view.setImageUrl(cursor.getString(3));
			// try
			// {
			// String imgUrl = cursor.getString(3);
			// if (imgUrl != null)
			// {
			// String imageName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1);
			// view.setBigImage(Helpers.getSmallAdviceImage(context,
			// imageName));
			// }
			// }
			// catch (IOException e)
			// {
			// }
			// String imgUrl = cursor.getString(3);
			// if (imgUrl != null)
			// {
			// view.setBigImage(Helpers.getImage(imgUrl));
			// }
		}
		if (cursor != null && !cursor.isClosed()) cursor.close();

		return view;
	}

}
